package com.open.api.wechat.pub.impl;


import com.open.api.wechat.common.WeChatClient;
import com.open.api.wechat.constants.WeChatAppUrlConstants;
import com.open.api.wechat.common.request.WeChatAccessTokenRequest;
import com.open.api.wechat.common.response.WeChatAccessTokenResponse;
import com.open.api.wechat.config.properties.WeChatPubProperties;
import com.open.api.wechat.pub.request.WeChatTemplateMessageRequest;
import com.open.api.wechat.pub.request.condition.WeChatTemplateMessageCondition;
import com.open.api.wechat.pub.response.WeChatTemplateMessageResponse;
import com.open.api.wechat.pub.WeChatPubService;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author : zhaoxuan
 * @date : 2021/6/30 14:27
 * @description : WeChatPubServiceImpl
 * @version: 1.0
 */
@Data
@AllArgsConstructor
public class WeChatPubServiceImpl implements WeChatPubService {

    private static final String APP_ID = "APPID";

    private static final String APP_SECRET = "APPSECRET";

    private WeChatPubProperties weChatPubProperties;

    @Override
    public WeChatAccessTokenResponse getAccessToken() {
        WeChatAccessTokenRequest request = new WeChatAccessTokenRequest();
        String url = WeChatAppUrlConstants.WE_CHAT_ACCESS_TOKEN
                .replace(APP_ID, weChatPubProperties.getAppId())
                .replace(APP_SECRET, weChatPubProperties.getAppSecret());
        request.setUrl(url);
        return WeChatClient.execute(request);
    }

    @Override
    public WeChatTemplateMessageResponse sendTemplateMessage(WeChatTemplateMessageCondition condition, String accessToken) {
        WeChatTemplateMessageRequest request = new WeChatTemplateMessageRequest();
        request.setCondition(condition);
        return WeChatClient.execute(request, accessToken);
    }
}
